;;; personal-common-darwin.el -- preferences/setup for Emacs running on
;;; Mac OS X (in GUI or terminal mode)

(setq
 mac-control-modifier 'control
 mac-command-modifier 'meta
 mac-option-modifier 'meta)

;; Establish a more useful working directory on launch
(when (equal default-directory "/")
  (cd (expand-file-name "~/")))

;; Integrate with the standard OS X trash directory
(setq
 delete-by-moving-to-trash t
 trash-directory (expand-file-name "~/.Trash"))

;; When Finder asks Emacs to open a file, don't create a new frame for it
(setq ns-pop-up-frames nil)

;; For some reason which I cannot begin to fathom, Emacs' default behavior
;; when a file is dragged from the Finder onto an Emacs frame is to insert
;; the file's contents at the spot where the mouse button is released. It
;; makes over 9000 times more sense just to open the file in a new buffer.
(define-key global-map [ns-drag-file] 'ns-find-file)

(defun register-path (path)
  (add-to-list 'exec-path path t)
  ;; Don't love having stuff that checks for window-system in a file that's
  ;; loaded by both terminal and GUI Emacs, but I need to add to exec-path for
  ;; both and I would like to keep the list of paths DRY.
  (if window-system
      (setenv "PATH" (concat (getenv "PATH") ":" path))))

(register-path (expand-file-name "~/Applications/bin"))
(register-path "/usr/local/bin")
(register-path "/usr/local/git/bin")

(defun dired-system-open ()
  (interactive)
  (save-window-excursion
    (dired-do-shell-command
     "open" current-prefix-arg
     (dired-get-marked-files t current-prefix-arg))))

(add-hook
 'dired-load-hook
 (lambda ()
   (define-key dired-mode-map (kbd "C-c C-o") 'dired-system-open)))

;; Have dired et al. use GNU version of ls if available
(when (executable-find "gls")
  (setq insert-directory-program "gls"))
